﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;
using System.Collections;
namespace BLL.IntelligenceSearch
{
    abstract public class ASearch
    {
        protected Hashtable fieldWeight = new Hashtable();
        protected Hashtable resultList = new Hashtable();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewname">表名或者试图名</param>
        /// <param name="fieldname">字段名</param>
        /// <param name="searchword">搜索词</param>
        protected void SearchDB( string viewname,
            string fieldname, string searchword,string stateinfo)
        {
            StringBuilder sqlsb = new StringBuilder();
            sqlsb.Append("select id, dblno,nickname,fullname,keyword1,keyword2,keyword3,infostate,basemark from ");
            sqlsb.Append(viewname);
            sqlsb.Append(" where ");
            if (!stateinfo.Equals(""))
            {
                sqlsb.Append("infostate = '" + stateinfo + "' and ");
            }
            sqlsb.Append(fieldname);
            sqlsb.Append(" like '%");
            sqlsb.Append(searchword);
            sqlsb.Append("%'");

            string sqlstr = sqlsb.ToString();

            DataSet ds = DbHelperOra.Query(sqlstr);
            if (ds.Tables[0].Rows.Count == 0)
            {
                //没有找到该关键字
                return;
            }
            else
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int basemark = Convert.ToInt32(dr["basemark"]);//或者这条记录的基础分数，一般来说金牌分数>>银牌>>普通会员

                    int id = Convert.ToInt32(dr["id"]);

                    int mark = GetUserMark(fieldname,basemark);
                    if (resultList.ContainsKey(id))
                    {
                        //如果包含这个键，就包含这个记录，那么就累加mark的值

                        ((BriefInfoComparable)resultList[id]).RelevantMark += mark;
                    }
                    else
                    {
                        //不包含，那么新增
                        BriefInfoComparable bi = new BriefInfoComparable();

                        bi.MemberID = Convert.ToInt32(dr["id"]);
                        bi.DBLNo = dr["dblno"].ToString();
                        bi.FullName = dr["fullname"].ToString();
                        bi.NickName = dr["nickname"].ToString();
                        bi.Keyword1 = dr["keyword1"].ToString();
                        bi.Keyword2 = dr["keyword2"].ToString();
                        bi.Keyword3 = dr["keyword3"].ToString();
                        bi.RelevantMark = mark;
                        bi.InfoState = dr["infostate"].ToString();
                        resultList.Add(id, bi);
                    }
                }
            }
            

        }
        /// <summary>
        /// 获得普通用户分数
        /// </summary>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        protected int GetUserMark(string fieldname,int basemark)
        {
            return Convert.ToInt32(fieldWeight[fieldname]) * basemark;
        }

        protected ArrayList SortResultList(Hashtable resultList)
        {
            ArrayList al = new ArrayList(resultList.Values);
            al.Sort();//按照分数从小到大排序
            al.Reverse();//翻转位从大到小
            return al;
        }

        abstract public ArrayList DoSearch();

        abstract public ArrayList DoSearchAll();
    }
}
